package com.custom.adz.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.View;

/**
 * Created by Administrator on 2017/6/20.
 */

public class CircleDotProgressBar extends View {
    private Paint mPaint;
    private float mSin_1; // sin(1°)
    private float mCenterX;
    private float mCenterY;
    public CircleDotProgressBar(Context context) {
        this(context, null);
    }
    public CircleDotProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CircleDotProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mSin_1 = (float) Math.sin(Math.toRadians(4)); // 求sin(1°)。角度需转换成弧度
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 消除锯齿
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);
        drawCircleDot(canvas);
    }
    /**
     * 绘制圆点进度
     * @param canvas 画布
     */
    private void drawCircleDot(Canvas canvas) {
        // 计算圆点半径
        float outerRadius = (getWidth() < getHeight() ? getWidth() : getHeight()) / 2f;
        mCenterX = getWidth() / 2f;
        mCenterY = getHeight() / 2f;

        // outerRadius = innerRadius + dotRadius
        // sin((360°/200)/2) = sin(0.9°) = dotRadius / innerRadius;
        // 为了让圆点饱满一些，把角度0.9°增加0.1°到1°
        float dotRadius = mSin_1 * outerRadius / (1 + mSin_1);
        // 画进度
        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.FILL);
        int count = 0;
        // 1.1 当前进度
        while (count++ < 6) {
            canvas.drawCircle(mCenterX, mCenterY - outerRadius + dotRadius, dotRadius, mPaint);
            canvas.rotate(-24f, mCenterX, mCenterY);
        }
        // 1.2 未完成进度
        mPaint.setColor(Color.GRAY);
        count--;
        while (count++ < 11) {
            if (count==7){
                canvas.rotate(-96f, mCenterX, mCenterY);
            }
            canvas.drawCircle(mCenterX, mCenterY - outerRadius + dotRadius, dotRadius, mPaint);
            canvas.rotate(-24f, mCenterX, mCenterY);
        }
    }
}
